package com.eyes.ds.display;

import android.app.Presentation;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.eyes.ds.R;

/**
 *
 * 展示界面
 *
 * Author Jayy.Lucas
 * Email: eyes.left@qq.com
 * Date: 2017/10/31
 * Time: 下午3:53
 */
public class Showoff extends Presentation {

    public static final Integer[] PHOTOS = new Integer[] {
            android.R.drawable.presence_video_online,
            R.drawable.photo1, R.drawable.photo2, R.drawable.photo3,
            R.drawable.photo4, R.drawable.photo5, R.drawable.photo6
    };

    public ShowContents mContents;

    public Showoff(Context context, Display display,
                   ShowContents contents) {
        super(context, display);
        mContents = contents;
    }

    /**
     * Sets the preferred display mode id for the presentation.
     */
    public void setPreferredDisplayMode(int modeId) {
        mContents.displayModeId = modeId;

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.preferredDisplayModeId = modeId;
        getWindow().setAttributes(params);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);

        // Get the resources for the context of the presentation.
        // Notice that we are getting the resources from the context of the presentation.
        Resources r = getContext().getResources();

        // Inflate the layout.
        setContentView(R.layout.showoff_content);

        final Display display = getDisplay();
        final int displayId = display.getDisplayId();
        final int photo = mContents.photo;

        // Show a caption to describe what's going on.
        TextView text = (TextView)findViewById(R.id.text);
        text.setText(r.getString(R.string.presentation_photo_text,
                photo, displayId, display.getName()));

        // Show a n image for visual interest.
        ImageView image = (ImageView)findViewById(R.id.image);
        image.setImageDrawable(r.getDrawable(photo));
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getOwnerActivity(),"you are not supposed to click see me...", Toast.LENGTH_LONG).show();
            }
        });

        /*GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);

        // Set the background to a random gradient.
        Point p = new Point();
        getDisplay().getSize(p);
        drawable.setGradientRadius(Math.max(p.x, p.y) / 2);
        drawable.setColors(mContents.colors);
        findViewById(android.R.id.content).setBackground(drawable);*/
    }

    /**
     * 副屏显示的内容
     */
    public final static class ShowContents implements Parcelable {
        public final int photo;
        final int[] colors;
        int displayModeId;

        public static final Creator<ShowContents> CREATOR =
                new Creator<ShowContents>() {
                    @Override
                    public ShowContents createFromParcel(Parcel in) {
                        return new ShowContents(in);
                    }

                    @Override
                    public ShowContents[] newArray(int size) {
                        return new ShowContents[size];
                    }
                };

        public ShowContents(int photo) {
            this.photo = photo;
            colors = new int[] {
                    ((int) (Math.random() * Integer.MAX_VALUE)) | 0xFF000000,
                    ((int) (Math.random() * Integer.MAX_VALUE)) | 0xFF000000 };
        }

        private ShowContents(Parcel in) {
            photo = in.readInt();
            colors = new int[] { in.readInt(), in.readInt() };
            displayModeId = in.readInt();
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(photo);
            dest.writeInt(colors[0]);
            dest.writeInt(colors[1]);
            dest.writeInt(displayModeId);
        }
    }

}
